<template>
  <div class='container'>
    <el-dialog
      title="修改学科"
      :visible.sync="isShow.show"
      width="25%">
      <el-form :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="活动名称" prop="subjectName">
          <el-input v-model="ruleForm.subjectName" @change="closeSwitch" size="small"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="ruleForm.isFrontDisplay" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('close', false)">取 消</el-button>
        <el-button type="primary" @click="editSub">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { update, add } from '@/api/hmmm/subjects.js'
export default {
  name: 'subject-add',
  props: ['isShow', 'ruleForm', 'control'],
  data () {
    return {
      ruleForm1: {},
      rules: {
        subjectName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.ruleForm1 = this.ruleForm
  },

  methods: {
    async editSub () {
      if (this.control) {
        // 发送编辑
        const res = await update({
          id: this.ruleForm.id,
          isFrontDisplay: this.ruleForm.isFrontDisplay,
          subjectName: this.ruleForm.subjectName
        })
        if (res.status === 200) {
          this.$message.success('操作成功')
          this.$emit('close', false)
        } else {
          this.$message.error('操作失败')
        }
      } else {
        // 发送添加
        const res = await add({
          id: this.ruleForm.id,
          isFrontDisplay: this.ruleForm.isFrontDisplay,
          subjectName: this.ruleForm.subjectName
        })
        if (res.status === 200) {
          this.$message.success('添加成功')
          this.$emit('close', false)
        } else {
          this.$message.error('添加失败')
        }
      }
    },
    closeSwitch () {

    }
  }
}
</script>
